package com.work.dyduser.module.pagetwo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.work.dyduser.R;
import com.work.dyduser.base.BaseActivity;
import com.work.dyduser.bean.CommonBean;
import com.work.dyduser.cache.Ckey;
import com.work.dyduser.cache.SPutils;
import com.work.dyduser.httpretrofit.MultipartBodyUtils;
import com.work.dyduser.httpretrofit.MyCallback;
import com.work.dyduser.httpretrofit.RetrofitManager;
import com.work.dyduser.module.main.MainActivity;
import com.work.dyduser.utils.LogUtils;
import com.work.dyduser.utils.UIUtils;
import com.work.dyduser.utils.ViewUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import retrofit2.Call;

/**
 * author: Wangzhan
 * date :2018/7/27 0027 16:28
 * version:2.0.0
 * description:播放视频
 */

public class PlayVideoActivity extends BaseActivity {
    private SurfaceView videoSurfaceView;
    private ImageView playVideo;
    private MediaPlayer mediaPlayer;
    private String filepath;
    private TextView tv_delete;
    private TextView tv_upload;
    private ImageView image;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pagetwo_playvideo;
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("视频预览");
        mTitleBar.titleBack.setVisibility(View.GONE);
        videoSurfaceView = findViewById(R.id.surfaceview);
        playVideo = findViewById(R.id.iv_play);
        tv_delete = findViewById(R.id.tv_delete);
        tv_upload = findViewById(R.id.tv_upload);
        ViewUtils.setOnClickListeners(this, tv_delete, tv_upload, playVideo);
    }



    @Override
    protected void initData() {
        filepath = getIntent().getExtras().getString("videoPath");

        LogUtils.d("视频大小 = " + getFileSize(filepath));

        MediaMetadataRetriever media = new MediaMetadataRetriever();
        if (filepath != null) {
            media.setDataSource(filepath);
            Bitmap bitmap = media.getFrameAtTime();
            image =this.findViewById(R.id.imageView);
            image.setImageBitmap(bitmap);
        }

    }


    /**
     * 播放视频
     */
    public void playVideo() {
        image.setVisibility(View.GONE);
        videoSurfaceView.setVisibility(View.VISIBLE);
        playVideo.setVisibility(View.GONE);
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(filepath);
            mediaPlayer.setDisplay(videoSurfaceView.getHolder());
            mediaPlayer.prepare();//缓冲
            mediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                playVideo.setVisibility(View.VISIBLE);
                image.setVisibility(View.VISIBLE);
                videoSurfaceView.setVisibility(View.GONE);
            }
        });
    }


    /**
     * 暂停播放
     */
    public void stopPlayVideo() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }


    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.iv_play:
                //播放
                if (!TextUtils.isEmpty(filepath)) {
                    playVideo();
                } else {
                    UIUtils.showToast("文件不存在");
                }
                break;
            case R.id.tv_delete:
                //c重新拍摄
//                deletefile(filepath);
                UIUtils.startActivity(new Intent(PlayVideoActivity.this, VideoActivity.class));
                finish();
                break;
            case R.id.tv_upload:
                //开始上传
                LogUtils.d("filepath=" + filepath);
                //开始上传
//                getHttpUpVideo(filepath);
                UIUtils.showToast("上传成功");
                MainActivity.rb_main_HomePage.setChecked(true);
                SPutils.remove(Ckey.USERID);
                finish();
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        //暂停播放
        stopPlayVideo();
    }


    private void setTime(Long time, String type) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        LogUtils.d(type + " = " + dateFormat.format(date));
    }


    private String getFileSize(String path) {
        File f = new File(path);
        if (!f.exists()) {
            return "0 MB";
        } else {
            long size = f.length();
            return (size / 1024f) / 1024f + "MB";
        }
    }


    //上传视频
    private void getHttpUpVideo(String filepath) {
        showLoaddialog("上传中");
        Call<CommonBean> call = RetrofitManager.getInstance().uploadFile(SPutils.get(Ckey.USERID), MultipartBodyUtils.getPart(filepath));
        call.enqueue(new MyCallback<CommonBean>() {

            @Override
            public void onSuccess(CommonBean response) {
                setTime(System.currentTimeMillis(), "上传结束时间");
                UIUtils.showToast("上传成功");
                MainActivity.rb_main_HomePage.setChecked(true);
                SPutils.remove(Ckey.USERID);
                finish();
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {

            }

            @Override
            public void onComplete() {
                hideLoaddialog();
            }
        });

    }

}
